package fhq.hcmute.modernshopping.activities;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import fhq.hcmute.modernshopping.R;
import fhq.hcmute.modernshopping.adapter.DisplayProductDrawerAdapter;
import fhq.hcmute.modernshopping.adapter.Drawer;
import fhq.hcmute.modernshopping.adapter.Product;
import fhq.hcmute.modernshopping.fragment.CommentDialog;
import fhq.hcmute.modernshopping.fragment.ProductComments;
import fhq.hcmute.modernshopping.fragment.ProductInStores;
import fhq.hcmute.modernshopping.fragment.ProductInfo;

public class DisplayProductDetailActivity extends Activity {
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private ArrayList<Drawer> listDrawers;
	private Product product;
	private int currentDrawer = Drawer.PRODUCT_INFO;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_product_detail);
		init();
		this.product = (Product) getIntent().getSerializableExtra(
				ScanQrCodeActivity.SCAN_QRCODE_RESULT);
		selectItem(Drawer.PRODUCT_INFO);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.product_comment_menu, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem item = menu.findItem(R.id.menu_item_product_comment);
		if (currentDrawer == Drawer.PRODUCT_COMMENTS) {
			item.setVisible(true);
		} else {
			item.setVisible(false);
		}
		return super.onPrepareOptionsMenu(menu);
	}

	private ArrayList<Drawer> createDrawers() {
		ArrayList<Drawer> drawers = new ArrayList<Drawer>();
		drawers.add(new Drawer(Drawer.PRODUCT_INFO, getResources().getDrawable(
				R.drawable.ic_drawer_info), getString(R.string.drawer_info)));
		// drawers.add(new Drawer(Drawer.PRODUCT_DETAILS, getResources()
		// .getDrawable(R.drawable.ic_drawer_details),
		// getString(R.string.drawer_details)));
		drawers.add(new Drawer(Drawer.PRODUCT_COMMENTS, getResources()
				.getDrawable(R.drawable.ic_drawer_comment),
				getString(R.string.drawer_comments)));
		drawers.add(new Drawer(Drawer.PRODUCT_LOCATIONS, getResources()
				.getDrawable(R.drawable.ic_drawer_location),
				getString(R.string.drawer_locations)));
		return drawers;
	}

	private void init() {
		listDrawers = createDrawers();
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		mDrawerList.setOnItemClickListener(onDrawerItemClickListener);
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);
		DisplayProductDrawerAdapter adapter = new DisplayProductDrawerAdapter(
				getApplicationContext(), listDrawers);
		mDrawerList.setAdapter(adapter);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close);
		mDrawerLayout.setDrawerListener(mDrawerToggle);
	}

	private OnItemClickListener onDrawerItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> list, View view, int position,
				long id) {
			mDrawerLayout.closeDrawer(mDrawerList);
			selectItem(position);
		}
	};

	public boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

	private void selectItem(int position) {
		Fragment fragment = null;
		if (position == Drawer.PRODUCT_INFO) {
			fragment = ProductInfo.getProductInfo(product);
			getActionBar().setTitle(
					getString(R.string.title_display_product_info));
			currentDrawer = Drawer.PRODUCT_INFO;
		} else if (position == Drawer.PRODUCT_COMMENTS) {
			if (isOnline()) {
				fragment = ProductComments.getProductComments(this, product);
				getActionBar().setTitle(
						getString(R.string.title_display_product_comments));
				currentDrawer = Drawer.PRODUCT_COMMENTS;
			} else {
				Toast.makeText(getApplicationContext(), "Lỗi mạng",
						Toast.LENGTH_SHORT).show();
			}
		} else if (position == Drawer.PRODUCT_LOCATIONS) {
			if (isOnline()) {
				fragment = ProductInStores.getProductInStores(this, product);
				getActionBar().setTitle(
						getString(R.string.title_display_product_locations));
				currentDrawer = Drawer.PRODUCT_LOCATIONS;
			} else {
				Toast.makeText(getApplicationContext(), "Lỗi mạng",
						Toast.LENGTH_SHORT).show();
			}
		}
		if (fragment != null) {
			invalidateOptionsMenu();
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();
			mDrawerList.setItemChecked(position, true);
			mDrawerLayout.closeDrawer(mDrawerList);
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ProductInStores.destroyInstance();
		ProductInfo.destroyInstance();
		ProductComments.destroyInstance();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_item_product_comment) {
			CommentDialog dialog = new CommentDialog(product.getCode(),
					getString(R.string.title_display_product_comments));
			dialog.show(getFragmentManager(), "dialog");
		}
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}
}
